/**
 * Created by afer on 2017/8/19.
 */

var JsonModel = require('../config/jsonModel');
var ICOInfodao = require('../Dao/ICOInfodao');
var Path = require('path');

var multiparty = require('multiparty');
var util = require('util');
var fs = require('fs');
var Uploadicocoinimage = require('./uploadImage').uploadicocoinimage;

module.exports = function (app) {
    app.post('/addICOInfo', function (req, res) {

        // name: {type: 'string'},                 // ICO 名称
        // iconimage: {type: 'string'},            // ICO LOGO
        // platformurl: {type: 'string'},          // 平台官网
        // ICOPrice: {type: 'string'},             // ICO 价格
        // nowPrice: {type: 'string'},             // 现在价格
        // allmarketvalue: {type: 'string'},       // 市场总价
        Uploadicocoinimage(req, res, function (path, fields) {
            var name = fields.name[0];
            var platformurl = fields.platformurl[0];
            var ICOPrice = fields.ICOPrice[0];
            var nowPrice = fields.nowPrice[0];
            var icodate = fields.icodate[0];
            var icodes = fields.icodes[0];
            var cointotalnum = fields.cointotalnum[0];
            var model = new JsonModel.JsonModel();
            model.status = 0;
            try {
                if (!name) {
                    throw  new Error('name 不能为空！');
                }
                if (!platformurl) {
                    throw  new Error('platformurl 不能为空！');
                }
                if (!ICOPrice) {
                    throw  new Error('ICOPrice 不能为空！');
                }
                if (!nowPrice) {
                    throw  new Error('nowPrice 不能为空！');
                }
                if (!icodate) {
                    throw  new Error('icodate 不能为空！');
                }
                if (!icodes) {
                    throw  new Error('icodes 不能为空！');
                }
                if (!cointotalnum) {
                    throw  new Error('cointotalnum 不能为空！');
                }
            } catch (error) {
                model.content = {};
                model.msg = error.message;
                return res.send(model);
            }
            var icoinfo = {
                name: name,
                platformurl: platformurl,
                ICOPrice: Number(ICOPrice),
                nowPrice: Number(nowPrice),
                cointotalnum: Number(cointotalnum),
                iconimage: path.replace(Path.resolve(__dirname, '..') + '/client', ''),
                icodes: icodes,
                icodate: icodate,
            };
            ICOInfodao.create(icoinfo).then(function (result) {
                var ss = result.ops[0];
                if (!ss) {
                    fs.unlinkSync(path); //删除文件
                    model.content = {};
                    model.msg = '创建失败!';
                    model.status = 0;
                    return res.send(model);
                }
                model.content = ss;
                model.status = 1;
                model.msg = '添加成功!';
                return res.send(model);
            }).catch(function (error) {
                console.log(error);
                fs.unlinkSync(path); //删除文件
                model.content = {};
                if (error.message.match('E11000 duplicate key')) {
                    model.msg = '币种重复!';
                } else {
                    model.msg = error.message;
                }
                model.status = 0;
                return res.send(model);
            });
        });
    });

    app.get('/getAllIcoInfo', function (req, res) {
        ICOInfodao.getAllICOInfo().then(function (icoinfo) {
            var model = new JsonModel.JsonModel();
            model.status = 1;
            model.content = {icoInfoList: icoinfo};
            return res.send(model);
        });
    });
}